package workexp.alg.question;

import org.junit.Test;

import java.util.Arrays;

public class MoveZeroes {
    public void moveZeroes(int[] nums) {
        int searchPointer=0;

        while(searchPointer<nums.length){
            if(nums[searchPointer]==0){
                int tarPointer=searchPointer;
                while(tarPointer<nums.length-1&&nums[tarPointer]==0){
                    tarPointer++;
                }
                    nums[searchPointer]=nums[tarPointer];
                    nums[tarPointer]=0;

            }

            searchPointer++;
        }

    }

    public void moveZeroes2(int[] nums) {
        int n = nums.length, left = 0, right = 0;
        while (right < n) {
            if (nums[right] != 0) {
                swap(nums, left, right);
                left++;
            }
            right++;
        }
    }

    public void swap(int[] nums, int left, int right) {
        int temp = nums[left];
        nums[left] = nums[right];
        nums[right] = temp;
    }
    @Test
    public void test(){
        int[] nums = {1,0,1};
//        moveZeroes(nums);
        moveZeroes2(nums);
        System.out.println(Arrays.toString(nums));
    }
}
